﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;

namespace LabManager.Models
{

    public class CauHinhModel
    {
        public List<ComportInfo> _lstComportInfo = new List<ComportInfo>();   
        string serializationFile = "Labconfig.dll";

        public CauHinhModel() { }

        public void Save()
        {            
            //Serialize
            using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                bformatter.Serialize(stream, _lstComportInfo);
            }
        }


        public List<ComportInfo> LoadConfig()
        {
            try
            {
                if (File.Exists(serializationFile))
                {
                    //Deserialize
                    using (Stream stream = File.Open(serializationFile, FileMode.Open))
                    {
                        var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        return (List<ComportInfo>)bformatter.Deserialize(stream);
                    }
                }
                else
                {
                    _lstComportInfo = new List<ComportInfo>(new ComportInfo[8]);
                    Save();

                    //Deserialize
                    using (Stream stream = File.Open(serializationFile, FileMode.Open))
                    {
                        var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        return (List<ComportInfo>)bformatter.Deserialize(stream);
                    }
                }
            }
            catch { return null; }
        }
    }


    [Serializable()]
    public class ComportInfo
    {
        public ComportInfo() { }

        public string LabMachineName { get; set; }
        public string portName { get; set; }
        public int _Baurate { get; set; }
        public int _DataBits { get; set; }
        public Parity _Parity { get; set; }
        public StopBits _StopBits { get; set; }

        public Handshake _HandShaking { get; set; }
    }


}
